const WxParse = require('../../../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginUserId: 0, //当前登录用户的id
    userId: '', //发评论用户id
    upOrNot: 0, // 0未点赞  1已点赞   
    collectOrNot: 0, // 0未收藏  1已收藏  
    strategyId: 0, //攻略id
    strategyInfo: [], //攻略详情
    tagIds: [], //标签id
    tags: [], //标签名
    commentCount: 0, //评论数
    commentInfo: [], //评论内容
    strategyUpCount: 0, // 点赞数
    strategyCollect: 0, //收藏数
    // anoStrategyInfo: [], //其他攻略推荐
    strategyInfoLike: [], //猜你喜欢的攻略信息
    comm: '', //评论内容
    up: [], //进入页面判断是否点赞过
    coll: [], //进入页面判断是否收藏过
    userAva: [], //查用户信息拿头像
  },
  // 喜欢/点赞
  changeUp() {
    if (this.data.upOrNot != 1) {
      // 对接后端数据库发请求...是否点赞改变
      this.setData({
        upOrNot: 1
      })
    } else {
      // 对接后端数据库发请求...是否点赞改变
      this.setData({
        upOrNot: 0
      })
    }
  },
  // 收藏
  changeCollect() {
    if (this.data.collectOrNot != 1) {
      // 对接后端数据库发请求...是否收藏改变
      this.setData({
        collectOrNot: 1
      })
    } else {
      // 对接后端数据库发请求...是否收藏改变
      this.setData({
        collectOrNot: 0
      })
    }
  },
  mess: function () {
    wx.createSelectorQuery().select('.allComment').boundingClientRect(function (rect) {
      // 假设你要滚动到的元素ID是'your-element-id'
      wx.pageScrollTo({
        scrollTop: rect.top, // 滚动到元素的顶部位置
        duration: 300 // 滚动动画的时长，‌单位ms
      });
    }).exec();
  },

  /**
   * 发评论函数
   */
  send(e) {
    // console.log('comm', this.data.comm)
    wx.request({
      url: 'http://localhost:8080/strategy/strategy_comment',
      method: 'POST',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId,
        userId: this.data.loginUserId,
        commentContent: this.data.comm
      },
      success: (resp) => {
        // console.log('评论成功resp', resp)
        if (resp.data.code == 200) {
          wx.showToast({
            title: '发布成功',
            icon: 'success',
            duration: 1500
          })
          this.setData({
            comm: ''
          })
        }
        this.getComment()
        this.getCommentInfo()
      }
    })
    // console.log("发布评论函数的接收对象", e)
  },
  getData() {
    // 根据标签id查名称  传过去多条话题中间表
    var k;
    // console.log("start")
    for (k = 0; k < this.data.tagIds.length; k++) {
      // console.log("start5555")
      var option = this.data.tagIds[k].subjectId
      wx.request({
        url: `http://localhost:8080/subject/subject_list/${option}`,
        method: 'GET',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {},
        success: (resp) => {
          // console.log('tags', resp.data.data.subjectName)
          this.setData({
            tags: this.data.tags.concat(resp.data.data.subjectName)
          });
        }
      })
    }
  },

  // 查询评论内容...   /strategy/strategy_comment
  getCommentInfo() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy_comment/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId
      },
      success: (resp) => {
        // console.log('commentInfo', resp.data.rows)
        this.setData({
          commentInfo: resp.data.rows
        });
      }
    })
  },

  // 攻略详情     
  getStrategy() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy/` + this.data.strategyId,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {},
      success: (resp) => {
        // console.log('strategyInfo', resp.data.data)
        // if (resp.data.data.id != this.data.strategyId) {
        //   this.setData({
        //     anoStrategyInfo: resp.data.data
        //   })
        // } else {
        this.setData({
          strategyInfo: resp.data.data,
        });
        // }

        // if (this.data.strategyInfo && this.data.strategyInfo.strategyContent) {
        //   WxParse.wxParse('info', 'html', this.data.strategyInfo.strategyContent, this, 15);
        // }
      }
    })
  },

  // 猜你喜欢的攻略    
  getLikeStrategy() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {},
      success: (resp) => {
        // console.log('strategyInfoLike', resp.data.rows)
        this.setData({
          strategyInfoLike: resp.data.rows.slice(0, 2),
        });
      }
    })
  },

  // 攻略对应的标签 拿到标签数组  用标签id查询标签名称 
  getSubject() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy_subject/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId
      },
      success: (resp) => {
        // console.log('tagIds', resp.data.rows)
        this.setData({
          tagIds: resp.data.rows
        });
        this.getData()
      }
    })
  },

  // 查询点赞数...   /strategy/strategy_up 
  getUpNum() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy_up/count`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId
      },
      success: (resp) => {
        // console.log('strategyUpCount', resp.data.data)
        this.setData({
          strategyUpCount: resp.data.data
        });
      }
    })
  },

  // 查询收藏数...   /strategy/strategy_collect  
  getCollectNum() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy_collect/count`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId
      },
      success: (resp) => {
        // console.log('strategyCollect', resp.data.data)
        this.setData({
          strategyCollect: resp.data.data
        });
      }
    })
  },

  // 查询评论数   /strategy/strategy_comment
  getComment() {
    wx.request({
      url: `http://localhost:8080/strategy/strategy_comment/count`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId
      },
      success: (resp) => {
        // console.log('commentCount', resp.data.data)
        this.setData({
          commentCount: resp.data.data
        });
      }
    })
  },

  getUpResult() {
    //查询当前用户是否点赞过
    wx.request({
      url: `http://localhost:8080/strategy/strategy_up/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId,
        userId: this.data.loginUserId
      },
      success: (resp) => {
        // console.log('查询是否已点赞', resp.data.length)
        this.setData({
          up: resp.data.length
        })
      },
      complete: () => {
        // 在这里执行攻略点赞成功后的处理逻辑    0 未点赞
        // console.log('攻略点赞成功，执行后续处理...up:', this.data.up);
        if (this.data.up != 0) { //已点赞
          this.setData({
            upOrNot: 1
          })
        } else {
          this.setData({
            upOrNot: 0
          })
        }
      }
    })
  },

  // 点赞加入数据库/取消点赞     /strategy/strategy_up   
  insertStrategyUp() {
    this.getUpResult()
    // 根据判断结果 处理点赞/取消点赞
    if (this.data.upOrNot != 0) { //已点赞  取消点赞
      wx.request({
        url: `http://localhost:8080/strategy/strategy_up/sandu`,
        method: 'DELETE',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          strategyId: this.data.strategyId,
          userId: this.data.loginUserId
        },
        success: (resp) => {
          // console.log('取消攻略点赞')
          this.getUpNum()
          this.setData({
            upOrNot: 0
          })
        }
      })
    } else { //未点赞 点赞
      wx.request({
        url: `http://localhost:8080/strategy/strategy_up`,
        method: 'POST',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          strategyId: this.data.strategyId,
          userId: this.data.loginUserId
        },
        success: (resp) => {
          // console.log('攻略点赞成功')
          this.getUpNum()
          this.setData({
            upOrNot: 1
          })
        }
      })
    }

  },
  getCollectResult() {
    //查询当前用户是否收藏过
    wx.request({
      url: `http://localhost:8080/strategy/strategy_collect/sandu`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        strategyId: this.data.strategyId,
        userId: this.data.loginUserId
      },
      success: (resp) => {
        console.log('查询是否已收藏', resp.data.length)
        this.setData({
          coll: resp.data.length
        })
      },
      complete: () => {
        // 在这里执行上传成功后的处理逻辑
        console.log('是否收藏查询成功，执行后续处理...up:', this.data.coll);
        if (this.data.coll != 0) {
          this.setData({
            collectOrNot: 1
          })
        } else {
          this.setData({
            collectOrNot: 0
          })
        }
      }
    })
  },
  // 点击加入收藏表数据库/取消收藏   /strategy/strategy_collect 
  insertStrategyCollect() {
    this.getCollectResult()
    if (this.data.collectOrNot != 0) { //1 已收藏 取消收藏
      wx.request({
        url: `http://localhost:8080/strategy/strategy_collect/condition`,
        method: 'DELETE',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          strategyId: this.data.strategyId,
          userId: this.data.loginUserId
        },
        success: (resp) => {
          console.log('取消攻略收藏')
          console.log('strategyId', this.data.strategyId)
          console.log('loginUserId', this.data.loginUserId)
          // console.log('userId', this.data.userId)
          console.log('是否收藏', resp.data)
          this.getCollectNum()
          this.setData({
            collectOrNot: 0
          })
        }
      })
    } else { //0 未收藏 加入收藏
      wx.request({
        url: `http://localhost:8080/strategy/strategy_collect`,
        method: 'POST',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          strategyId: this.data.strategyId,
          userId: this.data.loginUserId
        },
        success: (resp) => {
          console.log('攻略收藏成功')
          this.getCollectNum()
          this.setData({
            collectOrNot: 1
          })
        }
      })
    }
  },
  //查询用户头像
  getUserAvatar() {
    wx.request({
      url: `http://localhost:8080/system/user/list`,
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {},
      success: (resp) => {
        console.log('查询用户', resp.data.rows)
        this.setData({
          userAva: resp.data.rows
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var loginfo = getApp().globalData.loginfo;
    // console.log(loginfo)
    this.setData({
      loginUserId: loginfo.userId
    })

    // console.log('options', options) //options.id 攻略id
    this.setData({
      strategyId: options.id
    })
    console.log('options', options)

    this.getStrategy()
    this.getLikeStrategy()
    this.getLikeStrategy()
    this.getComment()
    this.getCommentInfo()
    this.getUpNum()
    this.getCollectNum()

    this.getUpResult()
    this.getCollectResult()

    this.getUserAvatar()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})